<template>
  <div class="app-container" style="padding: 0px;">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >添加仪表</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pointMeterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="仪表名称" align="center" prop="meterInfo.name" />
      <el-table-column label="型号" align="center" prop="meterInfo.model" />
      <el-table-column label="厂家" align="center" prop="meterInfo.factory" />
      <el-table-column label="使用时间" align="center" prop="useTime" />
      <el-table-column label="停止时间" align="center" prop="stopTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仪表信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="仪表" prop="meterId" v-if="isAdd">
              <el-select v-model="form.meterId" style="width: 370px;" placeholder="——— 请选择要添加的仪表 ———">
                <el-option
                  v-for="meterInfo in meterInfoList"
                  :key="meterInfo.id"
                  :label="`【${meterInfo.name}】=》${meterInfo.model}=》${meterInfo.factory}`"
                  :value="meterInfo.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="仪表" prop="meterId" v-else>
              <el-input v-model="form.meterInfo.name" readonly />
              <el-input v-model="form.meterId" style="display: none;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用时间" prop="useTime" v-if="isAdd">
              <el-date-picker :editable="false" :clearable="false" style="width: 140px;"
                              v-model="form.useTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择使用时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="使用时间" prop="useTime" v-else>
              <el-input v-model="form.useTime" style="width: 140px;" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="停止时间" prop="stopTime">
              <el-date-picker :editable="false" :clearable="false" style="width: 140px;"
                              v-model="form.stopTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择停止时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listPointMeter,delPointMeter,addPointMeter,getPointMeter,updatePointMeter } from "@/api/basic/pointMeter";
  import { listMeterInfo } from "@/api/basic/meterInfo";

  export default {
    name: "MeterConf",
    props: ["pointId"],
    components: {
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 仪表信息表格数据
        pointMeterList: [],
        meterInfoList:[],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        isAdd: true,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          pointId: this.pointId,
        },
        // 表单参数
        form: {
          meterInfo:{}
        },
        // 表单校验
        rules: {
          meterId: [
            { required: true, message: "仪表不能为空", trigger: "blur" }
          ],
          useTime: [
            { required: true, message: "使用时间不能为空", trigger: "blur" }
          ]
        }
      };
    },
    watch: {
      pointId(){
        this.queryParams.pointId = this.pointId;
        this.getList();
      }
    },
    created() {
      this.getList();
      this.getMeterInfos();
    },
    methods: {
      // 表单重置
      reset() {
        this.form = {
          pointId: null,
          meterId: null,
          useTime: null,
          stopTime: null,
        };
        this.resetForm("form");
      },
      /** 查询仪表信息列表 */
      getMeterInfos() {
        let obj = {
          pageNum: 1,
          pageSize: 1000,
        }
        listMeterInfo(obj).then(response => {
          this.meterInfoList = response.rows;
        });
      },
      /** 查询仪表信息列表 */
      getList() {
        this.loading = true;
        listPointMeter(this.queryParams).then(response => {
          this.pointMeterList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.isAdd = true;
        this.open = true;
        this.title = "添加监测站点仪表";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.isAdd = false;
        getPointMeter(row.id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改监测站点仪表";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updatePointMeter(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              this.form.pointId = this.pointId;
              addPointMeter(this.form).then(response => {
                this.msgSuccess("添加成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除仪表信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPointMeter(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },

    }
  };
</script>
